﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using WebMatrix.WebData;
using StackExchange.Profiling;

namespace Echegoyen
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            try
            {
                Bootstrapper.Initialise(); // Unity

                Database.SetInitializer(new TestInitializer());
                MyContext db = new MyContext();
                db.Database.Initialize(true);

                AreaRegistration.RegisterAllAreas();

                //WebApiConfig.Register(GlobalConfiguration.Configuration);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
                AuthConfig.RegisterAuth();
            }
            catch (Exception e)
            {
                DependencyResolver.Current.GetService<IMyLogger>().Error("Aplicación", "Error al inicializar la aplicación", e);
            }

        }


        protected void Application_BeginRequest()
        {
            MiniProfiler.Start(); 
        }

        protected void Application_EndRequest()
        {
            MiniProfiler miniProfiler = MiniProfiler.Current;
            MiniProfiler.Stop();
            if (miniProfiler != null)
            {
                DependencyResolver.Current.GetService<IMyLogger>().HttpPerformanceInfo("HttpPerformance", ""+ miniProfiler.Render(), null);
            }
        }

    }
}